<?php

namespace app\stores\controller\miniapp;

use app\BaseController;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\mall\GoodsSkuModel;
use app\beautiful\model\member\MemberModel;
use app\stores\model\cart\CartModel;
use think\facade\Db;

class Cart extends BaseController{

    protected $data = [];

    /**
     * 向购物车中添加/修改商品
     */
    public function save()
    {
        $goods_id = $this->request->param('goods_id/d',0);
        $sku_id = $this->request->param('sku_id/d',0);
        $num = $this->request->param('num/d',1);

        if($goods_id<1) return $this->error('产品不存在');
        if($num<1) return $this->error('数量不能小于1');
        $where = [
            'shop_id'=>$this->request->shopId,
            'goods_id'=>$goods_id,
            'is_delete'=>0
        ];
        // 获取商品信息
        $goodsModel = new GoodsModel();
        $goods = $goodsModel->getById($where);

        if (!$goods) {
            // 商品不存在
            return $this->error('产品不存在');
        }

        if($sku_id){
            $sku_where = $where;
            $sku_where['sku_id'] = $sku_id;
            $goodsSkuModel = new GoodsSkuModel();
            $sku = $goodsSkuModel->getById($sku_where);
            if(!$sku){
                return $this->error('产品不存在');
            }

        }
        //购物车是否存在
        $cart_where = [
            'shop_id'=>$this->request->shopId,
            'goods_id'=>$goods_id,
            'sku_id'=>$sku_id,
            'member_id'=>$this->request->memberId,
            'is_delete'=>0
        ];

        $cartModel = new CartModel();
        $cart = $cartModel->getCart($cart_where);
        if($cart){
            $data['num'] = $num ;
            $cartModel->saveCart(['cart_id'=>$cart['cart_id']],$data);
        }else{
            $data['shop_id'] = $this->request->shopId;
            $data['goods_id'] = $goods_id;
            $data['sku_id'] = $sku_id;
            $data['member_id'] = $this->request->memberId;
            $data['num'] = $num;
            $cartModel::create($data);
        }

        return $this->success('添加成功');
    }

    /**
     * 获取购物车中的商品信息
     */
    public function getCartList()
    {
        $cart_ids = $this->request->param('cart_ids/a',[]);
        $member = $this->request->member;
        if(!$member && $this->request->memberId){
            $member_model = new MemberModel();
            $member = $member_model->getById(['member_id'=>$this->request->memberId]);
        }

        $where[] = [
            ['c.shop_id','=',$this->request->shopId],
            ['c.member_id','=', $this->request->memberId],
            ['c.is_delete','=',0]
        ];

        if($cart_ids) $where[] = ['c.cart_id','In',$cart_ids];
        $order = ['c.add_time'=>'desc'];
        $page = $this->request->page;
        $limit = $this->request->limit;
        $cartModel = new CartModel();
        $list = $cartModel->getCartList($member,$where,$order,$page,$limit);
        return $this->success($list);
    }

    /**
     * 清空购物车
     *
     * @return void
     */
    public function del()
    {
        $cart_id = $this->request->param('cart_id/d',0);

        if($cart_id<1)  return $this->success('删除成功');

        $cartModel = new CartModel();
        $cartModel->saveCart(['cart_id'=>$cart_id,'member_id'=>$this->request->memberId],['is_delete'=>1]);

        return $this->success('删除成功');
    }
}

